package project.vip.services.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kernel.util.StringUtils;
import kernel.web.Page;
import kernel.web.PagedQueryDao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import project.vip.po.GiftCard;
import project.vip.po.Vip;
import project.vip.services.AdminGiftCardService;

public class AdminGiftCardServiceImpl extends HibernateDaoSupport implements AdminGiftCardService {

    private PagedQueryDao pagedQueryDao;

    
    @Override
    public GiftCard getById(Serializable id) {
        StringBuffer queryString = new StringBuffer(" FROM GiftCard where id=?");
        List<GiftCard> list = this.getHibernateTemplate().find(queryString.toString(), id);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
    
    @Override
    public List<GiftCard> getGiftCardList(String fromOpenId) {
        StringBuffer queryString = new StringBuffer(" FROM GiftCard WHERE fromOpenId = ? ORDER BY createTime DESC");
        return this.getHibernateTemplate().find(queryString.toString(),new Object[]{fromOpenId});
    }

    @Override
    public List<GiftCard> getGiftCardList(String fromOpenId,String fromPhone) {
        StringBuffer queryString = new StringBuffer(" FROM GiftCard WHERE fromOpenId = ? AND fromPhone = ? ORDER BY createTime DESC");
        return this.getHibernateTemplate().find(queryString.toString(),new Object[]{fromOpenId,fromPhone});
    }
    
    @Override
    public Page pagedQuery(int pageNo, int pageSize, String fromPhone) {
        StringBuffer queryString = new StringBuffer(" FROM GiftCard WHERE 1=1 ");

        Map<String, Object> parameters = new HashMap<String, Object>();
        if (!StringUtils.isNullOrEmpty(fromPhone)) {
            queryString.append(" and fromPhone  = :fromPhone");
            parameters.put("fromPhone", fromPhone);
        }
        queryString.append(" order by createTime desc ");
        Page page = pagedQueryDao.pagedQuery(pageNo, pageSize, queryString.toString(), parameters);
        return page;

    }

    public void setPagedQueryDao(PagedQueryDao pagedQueryDao) {
        this.pagedQueryDao = pagedQueryDao;
    }
    
    @Override
    public void save(GiftCard entity) {
        entity.setCreateTime(new Date());
        this.getHibernateTemplate().save(entity);

    }

    @Override
    public void update(GiftCard entity) {
        this.getHibernateTemplate().update(entity);

    }

}
